﻿#include "FITKAcousticsVTKGraphObjectSEAJunctionLines.h"
#include "FITKAcousticsVTKGraphActor.h"

// VTK
#include <vtkProperty.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkCompositeDataGeometryFilter.h>
#include <vtkTubeFilter.h>
#include <vtkExtractBlock.h>

// Data
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"

// Graph and filter
#include "FITKAcousticsVTKCommons.h"
#include "FITKAcousticsVTKSharedDataManager.h"
#include "FITKAcousticsVTKGraphObjectSEASelectMergedData.h"
#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActor.h"

namespace Exchange
{
    FITKAcousticsVTKGraphObjectSEAJunctionLines::FITKAcousticsVTKGraphObjectSEAJunctionLines(Acoustics::FITKAcousticsDiscModel* seaModel)
        : FITKAcousticsVTKGraphObjectSEAJunctions(seaModel)
    {
        if (!seaModel)
        {
            return;
        }

        // Intiailize the polygon offset.
        m_polygonOffset = FITKAcousticsVTKCommons::s_polygonOffsetSEAJunction;

        // Initialize the SEA junctions type.
        m_SEAJunctionTypeEnum = Acoustics::FITKAbstractJunction::JunctionType::SEALineJunction;

        // Intialize the filter.
        init();

        // Generate the multi block dataset.
        if (!generateDataSet())
        {
            return;
        }

        // Generate the actor.
        generateGraph();

        // Update the visibility.
        updateVisibility();

        // Save the data type.
        m_graphInfo.Type = FITKAcousticsVTKCommons::JunctionLine;
    }

    FITKAcousticsVTKGraphObjectSEAJunctionLines::~FITKAcousticsVTKGraphObjectSEAJunctionLines()
    {
        // Delete vtk object.
        deleteVtkObj(m_tubeFilter);
    }

    void FITKAcousticsVTKGraphObjectSEAJunctionLines::init()
    {
        this->Superclass::init();

        // Create the tube filter.
        m_tubeFilter = vtkTubeFilter::New();
        m_tubeFilter->SetCapping(true);
        m_tubeFilter->SetNumberOfSides(30);
        m_tubeFilter->SetSidesShareVertices(true);
        m_tubeFilter->SetInputConnection(m_comp2Poly->GetOutputPort());
    }

    bool FITKAcousticsVTKGraphObjectSEAJunctionLines::getDataVisibility()
    {
        // Get the runtime settings for visibility.
        bool showSEAJunc = this->Superclass::getDataVisibility();
        bool showJLine = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_JUNCTION_LINES, true);
        bool showSEAJLine = getRuntimeSettingAs<bool>(RTS_KEY_VISIBLE_SEA_JUNC_LINES, true);
        return showSEAJunc && showJLine && showSEAJLine;
    }

    bool FITKAcousticsVTKGraphObjectSEAJunctionLines::generateDataSet()
    {
        //// Initialize the input of filter.
        //vtkMultiBlockDataSet* mDataSet = FITKAcousticsVTKSharedDataManager::GetInstance()->
        //    getSEAJunctionsMultiDataSet(Acoustics::FITKSEAAbstractJunction::SEAJunctionType::Line);
        //if (!mDataSet)
        //{
        //    return false;
        //}

        //m_extractBlock->SetInputData(mDataSet);
        return true;
    }

    void FITKAcousticsVTKGraphObjectSEAJunctionLines::generateGraph()
    {
        // Create grid actor.
        m_fActor = FITKAcousticsVTKGraphActor::New();
        m_fActor->setGraphObject(this);
        m_fActor->setActorType(ActorType::EdgeActor);
        m_fActor->SetPickable(false);
        m_fActor->SetVisibility(true);
        m_fActor->GetProperty()->SetRepresentation(2);
        m_fActor->GetProperty()->SetPointSize(0.);
        m_fActor->GetProperty()->SetLineWidth(1.);
        m_fActor->GetProperty()->SetEdgeVisibility(false);
        m_fActor->setRelativeCoincidentTopologyPolygonOffsetParameters(m_polygonOffset);
        addActor(m_fActor);

        // Set the input connection.
        m_fActor->setInputConnection(m_tubeFilter->GetOutputPort());

        // Enable the color array.
        m_fActor->setScalarVisibility(true);
        m_fActor->setScalarModeToUseCellFieldData();       
        m_fActor->selectScalarArray(DATA_COLOR_ARRAY_NAME);
    }

    void FITKAcousticsVTKGraphObjectSEAJunctionLines::updateGlyphSizeByRatio()
    {
        if (!m_tubeFilter)
        {
            return;
        }

        // Calculate the glyph size.
        m_tubeRadius = FITKAcousticsVTKSharedDataManager::GetInstance()->getTubeWorldSizeByRatio();
        m_tubeFilter->SetRadius(m_tubeRadius);

        // Update the pipeline.       
        m_tubeFilter->Update();
    }
}   // namespace Exchange